﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Assist;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Revit;
using YArchitech.Revit.Electric.Common;
using YArchitech.Revit.RevitExtension;
using YJKDragCurveJig;
using HYElectricKernel;
using HYRElectric.PowerSystem;

namespace HYRElectric.SystemDiagram
{
	public class SystemDiagramDrawingKit
	{
		public SystemDiagramDrawingKit(ExternalCommandData cmdData, int viewScale, Color switchLegendColor, GraphicsStyle generatrixStyle, GraphicsStyle circuitLineStyle, GraphicsStyle fractionLineStyle, TextNoteType textNoteType)
		{
			this.m_Revit = cmdData;
			this.m_ViewScale = viewScale;
			this.m_SwitchLegendColor = switchLegendColor;
			this.m_Doc = this.m_Revit.Application.ActiveUIDocument.Document;
			this.m_GeneratrixStyle = generatrixStyle;
			this.m_CircuitLineStyle = circuitLineStyle;
			this.m_FractionStyle = fractionLineStyle;
			this.m_TextNoteType = textNoteType;
			this.m_SwitchWidth = AssistFunc.mmToFeet((double)(15 * viewScale));
		}

		public Result Drawing(SystemGraphSet systemGraphSet, List<string> systemInfos, BusInfo busInfo, LoopCommonInfo loopCommonInfo, List<LoopInfo> loopInfo, List<CellInfo> cells)
		{
			this.m_SwitchLegendMapping = cells;
			this.m_TextNoteHeight = AssistFunc.mmToFeet(systemGraphSet.TextHeight);
			bool isHorizontal = false;
			if (systemGraphSet.Vector == 0)
			{
				isHorizontal = true;
			}
			this.RemoveInvalidData(ref busInfo, ref loopInfo);
			this.CreateInLineInfo(systemGraphSet, busInfo, loopInfo.Count);
			this.CreateGeneratrixInfo();
			this.CreateLoopInfo(systemGraphSet, loopCommonInfo, loopInfo);
			this.CreateSystemInfo(isHorizontal, systemInfos);
			this.CreateBoxInfo();
			this.MirrorTransform(isHorizontal);
			this.MoveTransform(isHorizontal);
			XYZ move = null;
			if (!this.DragJig(ref move))
			{
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			if (!this.RealDrawing(move))
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		private void RemoveInvalidData(ref BusInfo busInfo, ref List<LoopInfo> loopInfo)
		{
			string strB = "空";
			for (int i = 0; i < busInfo.busCells.Count; i++)
			{
				if (busInfo.busCells[i].CompareTo(strB) == 0)
				{
					busInfo.busCells.RemoveAt(i);
					busInfo.busTags.RemoveAt(i);
					i--;
				}
			}
			bool flag = false;
			bool flag2 = false;
			bool flag3 = false;
			for (int i = 0; i < loopInfo.Count; i++)
			{
				if (loopInfo[i].cells[0].CompareTo(strB) != 0)
				{
					flag = true;
				}
				if (loopInfo[i].cells[1].CompareTo(strB) != 0)
				{
					flag2 = true;
				}
				if (loopInfo[i].cells[2].CompareTo(strB) != 0)
				{
					flag3 = true;
				}
			}
			for (int i = 0; i < loopInfo.Count; i++)
			{
				if (!flag3)
				{
					loopInfo[i].cells.RemoveAt(2);
				}
				if (!flag2)
				{
					loopInfo[i].cells.RemoveAt(1);
				}
				if (!flag)
				{
					loopInfo[i].cells.RemoveAt(0);
				}
			}
		}

		private void CreateSystemInfo(bool isHorizontal, List<string> systemInfos)
		{
			double num = this.m_TextNoteHeight * (double)this.m_ViewScale;
			double num2 = 0.0;
			foreach (string str in systemInfos)
			{
				double num3 = this.GetTextRealWidth(str) * (double)this.m_ViewScale;
				if (Geometry.GreaterThan(num3, num2))
				{
					num2 = num3;
				}
			}
			if (isHorizontal)
			{
				double num4 = ((double)systemInfos.Count + 6.5) * num;
				double num5 = this.m_GeneratrixLength * 0.5;
				if (Geometry.LessThan(num5, num4))
				{
					this.m_BoxTop += num4 - num5;
				}
				if (Geometry.LessThan(this.m_InLineLength, num2))
				{
					this.m_BoxLeft -= num2 - this.m_InLineLength;
				}
			}
			else
			{
				double num6 = (double)(systemInfos.Count + 16) * num;
				if (Geometry.LessThan(this.m_InLineLength, num6))
				{
					this.m_BoxLeft -= num6 - this.m_InLineLength;
				}
				double num7 = this.m_GeneratrixLength * 0.5;
				if (Geometry.LessThan(num7, num2))
				{
					this.m_BoxTop += num2 - num7;
				}
			}
			double num8 = this.m_SwitchWidth * 0.5;
			double num9 = num * 0.5;
			XYZ xyz = new XYZ(this.m_BoxLeft + num8, this.m_BoxTop - num8 - num9, 0.0);
			double textRealWidth = this.GetTextRealWidth(systemInfos[0]);
			double textRealWidth2 = this.GetTextRealWidth(systemInfos[1]);
			double num10 = (textRealWidth > textRealWidth2) ? textRealWidth : textRealWidth2;
			string text = systemInfos[0];
			XYZ xyz2 = xyz;
			XYZ origin = xyz2 + XYZ.BasisX * num10 * (double)this.m_ViewScale * 0.5;
			double textWidth = this.GetTextWidth(text);
            this.m_SystemInfoTextInfos.Add(new JigTextInfo(this.m_TextNoteType, origin, XYZ.BasisX, XYZ.BasisY, (TextAlignFlags)1152, textWidth, text));
			xyz2 -= XYZ.BasisY * this.m_TextNoteHeight * (double)this.m_ViewScale * 0.8;
			XYZ startPoint = xyz2;
			XYZ endPoint = xyz2 + XYZ.BasisX * (num10 * (double)this.m_ViewScale);
			this.m_FractionLineInfos = new JigEdgeInfo(startPoint, endPoint, 0.0);
			text = systemInfos[2];
			xyz2 -= XYZ.BasisY * this.m_TextNoteHeight * (double)this.m_ViewScale * 0.8;
			origin = xyz2 + XYZ.BasisX * num10 * (double)this.m_ViewScale * 0.5;
			textWidth = this.GetTextWidth(text);
            this.m_SystemInfoTextInfos.Add(new JigTextInfo(this.m_TextNoteType, origin, XYZ.BasisX, XYZ.BasisY, (TextAlignFlags)1152, textWidth, text));
			xyz2 -= XYZ.BasisY * this.m_TextNoteHeight * (double)this.m_ViewScale;
			for (int i = 3; i < systemInfos.Count; i++)
			{
				text = systemInfos[i];
				xyz2 -= XYZ.BasisY * this.m_TextNoteHeight * (double)this.m_ViewScale * 1.2;
				textWidth = this.GetTextWidth(text);
                this.m_SystemInfoTextInfos.Add(new JigTextInfo(this.m_TextNoteType, xyz2, XYZ.BasisX, XYZ.BasisY, (TextAlignFlags)1088, textWidth, text));
			}
		}

		private void CreateInLineInfo(SystemGraphSet systemGraphSet, BusInfo busInfo, int loopCount)
		{
			double num = AssistFunc.mmToFeet((double)systemGraphSet.LoopSpace);
			this.m_GeneratrixLength = (double)(loopCount + 1) * num;
			XYZ xyz = XYZ.Zero - XYZ.BasisY * this.m_GeneratrixLength * 0.5;
			double num2 = AssistFunc.mmToFeet((double)systemGraphSet.InLine);
			XYZ xyz2 = xyz + XYZ.BasisX * num2;
			this.m_CircuitLineInfos.Add(new JigEdgeInfo(xyz, xyz2, 0.0));
			this.m_InLineLength += num2;
			XYZ xyz3 = xyz - XYZ.BasisY * this.m_TextNoteHeight * (double)this.m_ViewScale;
			xyz3 += XYZ.BasisX * this.m_SwitchWidth * 0.8;
			string text = string.Format("进线: {0} {1}", busInfo.busModel, busInfo.busWiring);
			double textWidth = this.GetTextWidth(text);
            this.m_OtherTextInfos.Add(new JigTextInfo(this.m_TextNoteType, xyz3, XYZ.BasisX, XYZ.BasisY, (TextAlignFlags)1152, textWidth, text));
			this.m_BoxLeft = xyz2.X - this.m_SwitchWidth * 0.5;
			this.CreateSwitchInfo(busInfo.busCells, busInfo.busTags, xyz2, ref xyz);
			this.m_InLineLength += xyz.DistanceTo(xyz2);
			double num3 = AssistFunc.mmToFeet((double)systemGraphSet.InLink);
			xyz2 = xyz + XYZ.BasisX * num3;
			this.m_CircuitLineInfos.Add(new JigEdgeInfo(xyz, xyz2, 0.0));
			this.m_InLineLength += num3;
		}

		private void CreateGeneratrixInfo()
		{
			XYZ xyz = new XYZ(this.m_InLineLength, 0.0, 0.0);
			XYZ endPoint = xyz - XYZ.BasisY * this.m_GeneratrixLength;
			this.m_GeneratrixInfos = new JigEdgeInfo(xyz, endPoint, 0.0);
			this.m_BoxTop = this.m_SwitchWidth * 0.5;
			this.m_BoxBottom = -this.m_GeneratrixLength - this.m_SwitchWidth * 0.5;
		}

		private void CreateLoopInfo(SystemGraphSet systemGraphSet, LoopCommonInfo loopCommonInfo, List<LoopInfo> loopInfo)
		{
			double num = AssistFunc.mmToFeet((double)systemGraphSet.LoopSpace);
			double num2 = AssistFunc.mmToFeet((double)systemGraphSet.OutLine);
			double num3 = AssistFunc.mmToFeet((double)systemGraphSet.OutLink);
			XYZ xyz = new XYZ(this.m_InLineLength, -num, 0.0);
			for (int i = 0; i < loopInfo.Count; i++)
			{
				LoopInfo loopInfo2 = loopInfo[i];
				XYZ xyz2 = xyz - XYZ.BasisY * ((double)i * num);
				XYZ xyz3 = xyz2 + XYZ.BasisX * num3;
				this.m_CircuitLineInfos.Add(new JigEdgeInfo(xyz2, xyz3, 0.0));
				if (loopInfo2.phaseSequence.CompareTo("单相") != 0)
				{
					string text = loopInfo2.phaseSequence;
					if (loopInfo2.phaseSequence.CompareTo("三相") == 0)
					{
						text = "L1L2L3";
					}
					XYZ lineTopTextLocation = this.GetLineTopTextLocation(xyz2, xyz3, false);
					double textWidth = this.GetTextWidth(text);
                    this.m_OtherTextInfos.Add(new JigTextInfo(this.m_TextNoteType, lineTopTextLocation, XYZ.BasisX, XYZ.BasisY, (TextAlignFlags)1152, textWidth, text));
				}
				this.CreateSwitchInfo(loopInfo2.cells, loopInfo2.tags, xyz3, ref xyz2);
				if (Geometry.IsEqual(this.m_BoxRight, 0.0))
				{
					this.m_BoxRight = xyz2.X + this.m_SwitchWidth * 0.5;
				}
				xyz3 = xyz2 + XYZ.BasisX * num2;
				this.m_CircuitLineInfos.Add(new JigEdgeInfo(xyz2, xyz3, 0.0));
				string text2 = string.Concat(new string[]
				{
					loopInfo2.loopName,
					" ",
					loopCommonInfo.loopModel,
					" ",
					loopCommonInfo.loopWiring
				});
				XYZ lineTopTextLocation2 = this.GetLineTopTextLocation(xyz2, xyz3, false);
				double textWidth2 = this.GetTextWidth(text2);
                this.m_OtherTextInfos.Add(new JigTextInfo(this.m_TextNoteType, lineTopTextLocation2, XYZ.BasisX, XYZ.BasisY, (TextAlignFlags)1152, textWidth2, text2));
				string text3;
				if (loopInfo2.use == "备用")
				{
					text3 = loopInfo2.use;
				}
				else
				{
					text3 = loopInfo2.load.ToString("f1") + "kW " + loopInfo2.use;
				}
				this.GetTextRealWidth(text3);
				int viewScale = this.m_ViewScale;
				XYZ origin = xyz3 + XYZ.BasisX * this.m_SwitchWidth * 0.8;
				double textWidth3 = this.GetTextWidth(text3);
                this.m_OtherTextInfos.Add(new JigTextInfo(this.m_TextNoteType, origin, XYZ.BasisX, XYZ.BasisY, (TextAlignFlags)1152, textWidth3, text3));
			}
		}

		private void CreateBoxInfo()
		{
			XYZ startPoint = new XYZ(this.m_BoxLeft, this.m_BoxTop, 0.0);
			XYZ endPoint = new XYZ(this.m_BoxLeft, this.m_BoxBottom, 0.0);
			this.m_BoxLineInfos.Add(new JigEdgeInfo(startPoint, endPoint, 0.0));
			startPoint = new XYZ(this.m_BoxLeft, this.m_BoxBottom, 0.0);
			endPoint = new XYZ(this.m_BoxRight, this.m_BoxBottom, 0.0);
			this.m_BoxLineInfos.Add(new JigEdgeInfo(startPoint, endPoint, 0.0));
			startPoint = new XYZ(this.m_BoxRight, this.m_BoxBottom, 0.0);
			endPoint = new XYZ(this.m_BoxRight, this.m_BoxTop, 0.0);
			this.m_BoxLineInfos.Add(new JigEdgeInfo(startPoint, endPoint, 0.0));
			startPoint = new XYZ(this.m_BoxRight, this.m_BoxTop, 0.0);
			endPoint = new XYZ(this.m_BoxLeft, this.m_BoxTop, 0.0);
			this.m_BoxLineInfos.Add(new JigEdgeInfo(startPoint, endPoint, 0.0));
		}

		private void MoveTransform(bool isHorizontal)
		{
			XYZ vector;
			if (isHorizontal)
			{
				vector = new XYZ(0.0, this.m_GeneratrixLength * 0.5, 0.0);
			}
			else
			{
				vector = new XYZ(-this.m_GeneratrixLength * 0.5, 0.0, 0.0);
			}
			Transform matrix = RevitVersionFuncs.CreateTranslation(vector);
			this.m_GeneratrixInfos.TransformSelf(matrix);
			this.m_FractionLineInfos.TransformSelf(matrix);
			for (int i = 0; i < this.m_CircuitLineInfos.Count; i++)
			{
				this.m_CircuitLineInfos[i].TransformSelf(matrix);
			}
			for (int i = 0; i < this.m_BoxLineInfos.Count; i++)
			{
				this.m_BoxLineInfos[i].TransformSelf(matrix);
			}
			for (int i = 0; i < this.m_SystemInfoTextInfos.Count; i++)
			{
				this.m_SystemInfoTextInfos[i].TransformSelf(matrix, false);
			}
			for (int i = 0; i < this.m_OtherTextInfos.Count; i++)
			{
				this.m_OtherTextInfos[i].TransformSelf(matrix, false);
			}
			for (int i = 0; i < this.m_SwitchLegendInfos.Count; i++)
			{
				this.m_SwitchLegendInfos[i].TransformSelf(matrix, false);
			}
		}

		private void MirrorTransform(bool isHorizontal)
		{
			if (isHorizontal)
			{
				return;
			}
			Transform transform = RevitVersionFuncs.CreateReflection(RevitVersionFuncs.CreatePlanByNormalAndOrigin(new XYZ(1.0, 1.0, 0.0), XYZ.Zero));
			this.m_GeneratrixInfos.TransformSelf(transform);
			for (int i = 0; i < this.m_CircuitLineInfos.Count; i++)
			{
				this.m_CircuitLineInfos[i].TransformSelf(transform);
			}
			for (int i = 0; i < this.m_BoxLineInfos.Count; i++)
			{
				this.m_BoxLineInfos[i].TransformSelf(transform);
			}
			for (int i = 0; i < this.m_OtherTextInfos.Count; i++)
			{
				this.m_OtherTextInfos[i].TransformSelf(transform, true);
			}
			for (int i = 0; i < this.m_SwitchLegendInfos.Count; i++)
			{
				this.m_SwitchLegendInfos[i].TransformSelf(transform, true);
			}
			XYZ xyz = new XYZ(this.m_BoxLeft, this.m_BoxTop, 0.0);
			Transform matrix = RevitVersionFuncs.CreateTranslation(Geometry.TransformPoint(xyz, transform) - xyz);
			for (int i = 0; i < this.m_SystemInfoTextInfos.Count; i++)
			{
				this.m_SystemInfoTextInfos[i].TransformSelf(matrix, false);
			}
			this.m_FractionLineInfos.TransformSelf(matrix);
		}

		private void CreateSwitchInfo(List<string> cells, List<string> tags, XYZ ptStart, ref XYZ ptEnd)
		{
			XYZ xyz = ptStart;
			for (int i = 0; i < cells.Count; i++)
			{
				AnnotationSymbolType switchType = this.GetSwitchType(cells[i]);
				XYZ xyz2 = xyz;
				XYZ xyz3 = xyz + XYZ.BasisX * this.m_SwitchWidth;
				if (switchType == null)
				{
					this.m_CircuitLineInfos.Add(new JigEdgeInfo(xyz2, xyz3, 0.0));
				}
				else
				{
					this.m_SwitchLegendInfos.Add(new SystemDiagramDrawingKit.SwitchLocation(switchType, xyz2, XYZ.BasisX, XYZ.BasisY));
				}
				string text = tags[i];
				XYZ lineTopTextLocation = this.GetLineTopTextLocation(xyz2, xyz3, true);
				double textWidth = this.GetTextWidth(text);
                this.m_OtherTextInfos.Add(new JigTextInfo(this.m_TextNoteType, lineTopTextLocation, XYZ.BasisX, XYZ.BasisY, (TextAlignFlags)1152, textWidth, text));
				xyz = xyz3;
				if (i < cells.Count - 1)
				{
					xyz3 = xyz + XYZ.BasisX * this.m_SwitchWidth;
					this.m_CircuitLineInfos.Add(new JigEdgeInfo(xyz, xyz3, 0.0));
					xyz = xyz3;
				}
			}
			ptEnd = xyz;
		}

		private AnnotationSymbolType GetSwitchType(string switchName)
		{
			if (switchName.CompareTo("空") == 0)
			{
				return null;
			}
			List<AnnotationSymbolType> list = (from r in (from ele in RevitFilter.GetElementsOfType(this.m_Doc, typeof(FamilySymbol)).OfCategory(BuiltInCategory.OST_GenericAnnotation)
			select (AnnotationSymbolType)ele).ToList<AnnotationSymbolType>()
			where r.Name.IndexOf(switchName) != -1
			select r).ToList<AnnotationSymbolType>();
			if (list.Count == 0)
			{
				if (!this.LoadSwitchLegendFamily(switchName))
				{
					return null;
				}
				list = (from r in (from ele in RevitFilter.GetElementsOfType(this.m_Doc, typeof(FamilySymbol)).OfCategory(BuiltInCategory.OST_GenericAnnotation)
				select (AnnotationSymbolType)ele).ToList<AnnotationSymbolType>()
				where r.Name.IndexOf(switchName) != -1
				select r).ToList<AnnotationSymbolType>();
			}
			return list[0];
		}

		public bool LoadSwitchLegendFamily(string switchName)
		{
			bool result = false;
			string cellFamilyByName = DistributionBoxCommon.GetCellFamilyByName(switchName, this.m_SwitchLegendMapping);
			if (!File.Exists(cellFamilyByName))
			{
				return false;
			}
			Transaction transaction = new Transaction(this.m_Doc);
			try
			{
				transaction.Start("loadFamily");
				result = this.m_Doc.LoadFamily(cellFamilyByName);
				transaction.Commit();
			}
			catch
			{
				transaction.RollBack();
				return false;
			}
			return result;
		}

		private bool DragJig(ref XYZ ptPos)
		{
			List<JigEdgeInfo> list = new List<JigEdgeInfo>();
			list.Add(this.m_GeneratrixInfos);
			list.Add(this.m_FractionLineInfos);
			list.AddRange(this.m_CircuitLineInfos);
			list.AddRange(this.m_BoxLineInfos);
			List<JigTextInfo> list2 = new List<JigTextInfo>();
			list2.AddRange(this.m_SystemInfoTextInfos);
			list2.AddRange(this.m_OtherTextInfos);
			try
			{
				if (!DragCurveJigInterface.GetInsertPosition(this.m_Revit, list, false, list2, false, XYZ.Zero, ref ptPos))
				{
					return false;
				}
			}
			catch (Exception)
			{
				return false;
			}
			return true;
		}

		private bool RealDrawing(XYZ move)
		{
			Transaction transaction = new Transaction(this.m_Doc);
			bool result;
			try
			{
				transaction.Start("drawing");
				Application application = this.m_Revit.Application.Application;
				Transform matrix = RevitVersionFuncs.CreateTranslation(move);
				foreach (JigEdgeInfo jigEdgeInfo in this.m_CircuitLineInfos)
				{
					jigEdgeInfo.TransformSelf(matrix);
					Curve curve = jigEdgeInfo.ConvertTo(application);
					this.DrawCurve(curve, this.m_CircuitLineStyle);
				}
				List<ElementId> list = new List<ElementId>();
				foreach (JigEdgeInfo jigEdgeInfo2 in this.m_BoxLineInfos)
				{
					jigEdgeInfo2.TransformSelf(matrix);
					Curve curve2 = jigEdgeInfo2.ConvertTo(application);
					DetailCurve detailCurve = this.DrawCurve(curve2, this.m_CircuitLineStyle);
					list.Add(detailCurve.Id);
				}
				LinePatternElement linePatternElementByName = LinePatternElement.GetLinePatternElementByName(this.m_Doc, "划线");
				if (linePatternElementByName == null)
				{
					linePatternElementByName = LinePatternElement.GetLinePatternElementByName(this.m_Doc, "Dash");
				}
				if (linePatternElementByName != null)
				{
					this.m_Doc.ActiveView.SetYJKProjLinePatternOverrideByElement(list, linePatternElementByName);
				}
				foreach (JigTextInfo jigTextInfo in this.m_SystemInfoTextInfos)
				{
					jigTextInfo.TransformSelf(matrix, false);
					this.DrawTextNote(jigTextInfo);
				}
				foreach (JigTextInfo jigTextInfo2 in this.m_OtherTextInfos)
				{
					jigTextInfo2.TransformSelf(matrix, false);
					this.DrawTextNote(jigTextInfo2);
				}
				List<ElementId> list2 = new List<ElementId>();
				foreach (SystemDiagramDrawingKit.SwitchLocation switchLocation in this.m_SwitchLegendInfos)
				{
					switchLocation.TransformSelf(matrix, false);
					AnnotationSymbol annotationSymbol = this.CreateSwitch(switchLocation);
					list2.Add(annotationSymbol.Id);
				}
				if (list2.Count > 0)
				{
					this.m_Doc.ActiveView.SetYJKProjColorOverrideByElement(list2, this.m_SwitchLegendColor);
				}
				this.m_GeneratrixInfos.TransformSelf(matrix);
				Curve curve3 = this.m_GeneratrixInfos.ConvertTo(application);
				this.DrawCurve(curve3, this.m_GeneratrixStyle);
				this.m_FractionLineInfos.TransformSelf(matrix);
				Curve curve4 = this.m_FractionLineInfos.ConvertTo(application);
				this.DrawCurve(curve4, this.m_FractionStyle);
				transaction.Commit();
				result = true;
			}
			catch (Exception)
			{
				transaction.RollBack();
				result = false;
			}
			return result;
		}

		private DetailCurve DrawCurve(Curve curve, GraphicsStyle lineStyle)
		{
			DetailCurve result;
			try
			{
                Autodesk.Revit.DB.View activeView = this.m_Revit.Application.ActiveUIDocument.ActiveView;
				DetailCurve detailCurve = this.m_Revit.Application.ActiveUIDocument.Document.Create.NewDetailCurve(activeView, curve);
				if (detailCurve != null)
				{
					detailCurve.LineStyle = lineStyle;
				}
				result = detailCurve;
			}
			catch (Exception)
			{
				result = null;
			}
			return result;
		}

		private TextNote DrawTextNote(JigTextInfo textInfo)
		{
			if (textInfo.StrText.CompareTo("") == 0)
			{
				return null;
			}
			TextNote result;
			try
			{
				XYZ upVec = Geometry.RotateTo(textInfo.BaseVec, Math.PI*.5, XYZ.BasisZ);
				TextNote textNode = this.m_Doc.GetTextNode(this.m_Doc.ActiveView, textInfo.Origin, textInfo.BaseVec, upVec, textInfo.LineWidth, textInfo.TextAlign, textInfo.StrText);
				textNode.TextNoteType = textInfo.theTextNoteType;
				result = textNode;
			}
			catch (Exception)
			{
				result = null;
			}
			return result;
		}

		private AnnotationSymbol CreateSwitch(SystemDiagramDrawingKit.SwitchLocation switchInfo)
		{
			AnnotationSymbol annotationSymbol = this.m_Doc.Create.NewFamilyInstance(switchInfo.m_Origin, switchInfo.m_LegendType, this.m_Doc.ActiveView) as AnnotationSymbol;
			if (switchInfo.m_BaseVec.IsAlmostEqualTo(XYZ.BasisX))
			{
				return annotationSymbol;
			}
			Line line = YJKLineEx.YJKGetUnBound(switchInfo.m_Origin, XYZ.BasisZ);
			ElementTransformUtils.RotateElement(this.m_Doc, annotationSymbol.Id, line, -Math.PI*.5);
			return annotationSymbol;
		}

		private XYZ GetLineTopTextLocation(XYZ ptStart, XYZ ptEnd, bool isOnLegend = false)
		{
			XYZ xyz = XYZ.BasisY * this.m_TextNoteHeight * (double)this.m_ViewScale;
			if (isOnLegend)
			{
				xyz = XYZ.BasisY * this.m_TextNoteHeight * (double)this.m_ViewScale * 2.0;
			}
			return Geometry.CalculatMidPoint(ptStart, ptEnd) + xyz;
		}

		private double GetTextWidth(string str)
		{
			return (double)str.Length * this.m_TextNoteHeight * this.m_TextWHScale * 2.0;
		}

		private double GetTextRealWidth(string str)
		{
			return (double)Encoding.Default.GetBytes(str).Length * this.m_TextNoteHeight * this.m_TextWHScale;
		}

		private ExternalCommandData m_Revit;

		private Document m_Doc;

		private GraphicsStyle m_FractionStyle;

		private GraphicsStyle m_GeneratrixStyle;

		private GraphicsStyle m_CircuitLineStyle;

		private TextNoteType m_TextNoteType;

		private List<CellInfo> m_SwitchLegendMapping;

		private JigEdgeInfo m_FractionLineInfos;

		private JigEdgeInfo m_GeneratrixInfos;

		private List<JigEdgeInfo> m_CircuitLineInfos = new List<JigEdgeInfo>();

		private List<JigEdgeInfo> m_BoxLineInfos = new List<JigEdgeInfo>();

		private List<JigTextInfo> m_SystemInfoTextInfos = new List<JigTextInfo>();

		private List<JigTextInfo> m_OtherTextInfos = new List<JigTextInfo>();

		private List<SystemDiagramDrawingKit.SwitchLocation> m_SwitchLegendInfos = new List<SystemDiagramDrawingKit.SwitchLocation>();

		private double m_SwitchWidth;

		private double m_GeneratrixLength;

		private double m_InLineLength;

		private double m_TextNoteHeight;

		private double m_TextWHScale = 0.7;

		private int m_ViewScale;

		private Color m_SwitchLegendColor;

		private double m_BoxLeft;

		private double m_BoxRight;

		private double m_BoxBottom;

		private double m_BoxTop;

		private class SwitchLocation
		{
			public SwitchLocation(SystemDiagramDrawingKit.SwitchLocation rhs)
			{
				this.m_LegendType = rhs.m_LegendType;
				this.m_Origin = rhs.m_Origin;
				this.m_BaseVec = rhs.m_BaseVec;
				this.m_UpVec = rhs.m_UpVec;
			}

			public SwitchLocation(AnnotationSymbolType legendType, XYZ origin, XYZ baseVec, XYZ upVec)
			{
				this.m_LegendType = legendType;
				this.m_Origin = origin;
				this.m_BaseVec = baseVec;
				this.m_UpVec = upVec;
			}

			public void TransformSelf(Transform matrix, bool transformCoordinateSystem = false)
			{
				this.m_Origin = JigGeometry.TransformPoint(this.m_Origin, matrix);
				if (transformCoordinateSystem)
				{
					this.m_BaseVec = JigGeometry.TransformPoint(this.m_BaseVec, matrix);
					this.m_UpVec = JigGeometry.TransformPoint(this.m_UpVec, matrix);
				}
			}

			public AnnotationSymbolType m_LegendType;

			public XYZ m_Origin;

			public XYZ m_BaseVec;

			public XYZ m_UpVec;
		}
	}
}
